package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Brand;
import com.itheima.exception.BusinessException;
import com.itheima.service.IBrandService;
import com.itheima.vo.PageBeanVo;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UmsBrandManager {

    @DubboReference
    private IBrandService brandService;

    //获取全部品牌列表
    public List<Brand> listAll() {
        return brandService.findAllBrand();
    }

    //根据品牌名称分页获取品牌列表
    public PageBeanVo list(Integer pageNum, Integer pageSize, String keyword) {
        Page page = brandService.pageByKeyword(pageNum, pageSize, keyword);
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

    //根据编号查询品牌信息
    public Brand query(Long id) {
        return brandService.findById(id);
    }

    //更新品牌
    public Integer updateBrand(Long id, Brand brand) {
        brand.setId(id);
        return brandService.updateBrand(brand);
    }

    //批量更新显示状态
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Brand brand = new Brand();
                brand.setId(id);
                brand.setShowStatus(showStatus);
                Integer count = brandService.updateBrand(brand);
                if (count == null) {
                    throw new BusinessException("更新出错");
                }
            }
        }
    }

    //批量更新厂家制造商状态
    public void updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Brand brand = new Brand();
                brand.setId(id);
                brand.setFactoryStatus(factoryStatus);
                Integer count = brandService.updateBrand(brand);
                if (count == null) {
                    throw new BusinessException("批量更新厂家制造商状态出错");
                }
            }
        }
    }

    //批量删除品牌
    public Integer deleteBatch(List<Long> ids) {
        return brandService.deleteByIds(ids);
    }

    //删除指定品牌
    public Integer deleteBrand(Long id) {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return brandService.deleteByIds(list);
    }

    //添加品牌
    public Integer create(Brand brand) {
        return brandService.saveBrand(brand);
    }

}
